/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ing.sisval.da;

import java.sql.Connection;
import java.util.ArrayList;
import org.ing.sisval.be.BEDistribucion;
import org.rx.cr.dao.DAOAbstract;

/**
 *
 * @author Mysham
 */
public class DADistribucion extends DAOAbstract<BEDistribucion> {

    public DADistribucion(Connection conn) {
        setConnection(conn);
    }

    @Override
    public int registrar(BEDistribucion be) throws Exception {
        try {
            setStoreProcedure("SISVAL.sp_ins_distribucion");
            setParameterString(be.getNombre());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int actualizarRegistro(BEDistribucion be) throws Exception {
        try {
            setStoreProcedure("SISVAL.sp_upd_distribucion");
            setParameterInt(be.getDistribucionid());
            setParameterString(be.getNombre());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int eliminarRegistro(BEDistribucion tipo) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    //@Override
    @Override
    public ArrayList<BEDistribucion> buscarReferencia(String string) throws Exception {
        try {
            clear();
            setStoreProcedure("SISVAL.sp_sel_distribucion");
            setParameterString(string);
            commitc();
            while (existResult()) {
                BEDistribucion be = new BEDistribucion();
                be.setDistribucionid(getDataInt(1));
                be.setNombre(getDataString(2));

                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public ArrayList<BEDistribucion> listar() throws Exception {
        try {
            clear();
            setStoreProcedure("SISVAL.sp_sel_distribucion");
            setParameterString("");
            commitc();
            while (existResult()) {
                BEDistribucion be = new BEDistribucion();
                be.setDistribucionid(getDataInt(1));
                be.setNombre(getDataString(2));

                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    //@Override
////    public ArrayList<BEDistribucion> listar(int id) throws Exception {
////        try {
////            clear();
////            setStoreProcedure("SISVAL.sp_sel_distribucion");
////            setParameterInt(id);
////            commitc();
////            while (existResult()) {
////                BEDistribucion be = new BEDistribucion();
////                be.setDistribucionid(getDataInt(1));
////                be.setNombre(getDataString(2));
////
////                add(be);
////            }
////            return getLista();
////        } catch (Exception e) {
////            rollback(e);
////            return null;
////        }
////    }
    @Override
    public BEDistribucion buscar(int i) throws Exception {
        try {
            BEDistribucion be = null;
            setStoreProcedure("SISVAL.sp_sel_distribucion_id");
            setParameterInt(i);
            commitc();
            while (existResult()) {
                be = new BEDistribucion();
                be.setDistribucionid(getDataInt(1));
                be.setNombre(getDataString(2));
               

            }
           
            return be;
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

}
